﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WD.Db.Contract;
using WD.Db.Contract.Model;
using WD.Framework.Contract;
using WD.Web.MVC.Common;
using WD.Web.MVC.Controllers.Filters;

namespace WD.Web.MVC.Areas.BaseArea.Controllers
{
    public class CodeDataController : AdminControllerBase
    {
        //
        // GET: /BaseArea/CodeData/
        [AdminOnlineFilter]
        public ActionResult Index(BaseRequest request)
        {
            request.ShowDeleted = true;
            request.Include = new List<string>
                {
                    "Base_CodeData_Parent"
                };
            var result = Base_CodeDataService.GetBase_CodeDataList(request);
            return View(result);
        }

        [AdminOnlineFilter]
        public ActionResult Create()
        {
            var model = new Base_CodeData();
            //读取缓存数据
            var list = CommonCache.CodeDataByCache.Select(x => x.Value).OrderBy(x => x.AllId).ToList();

            foreach (var item in list)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectList = new SelectList(list, "CodeID", "CodeName");
            ViewBag.list = selectList;
            return View("Create", model);
        }

        [HttpPost]
        [AdminOnlineFilter]
        public string Create(FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = new Base_CodeData();
            TryUpdateModel(model);
            try
            {
                Base_CodeDataService.SaveBase_CodeData(model, LoginInfo.LoginId);

                CommonCache.CodeDataByCache = null;         //清空缓存
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }
            return ajax.Suc("新增信息成功!");
        }

        [AdminOnlineFilter(Url = "/BaseArea/CodeData/Edit")]
        public ActionResult Edit(int id)
        {
            var model = Base_CodeDataService.GetBase_CodeData(id);
            //读取缓存数据
            var list = CommonCache.CodeDataByCache.Select(x => x.Value).OrderBy(x => x.AllId).ToList();
            foreach (var item in list)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectList = new SelectList(list, "CodeID", "CodeName", model.CodeType);
            ViewBag.list = selectList;
            return View("Edit", model);
        }

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/CodeData/Edit_Save")]
        public string Edit(int id, FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = Base_CodeDataService.GetBase_CodeData(id);
            TryUpdateModel(model);
            Base_CodeDataService.SaveBase_CodeData(model, LoginInfo.LoginId);
            CommonCache.CodeDataByCache = null;
            return ajax.Suc("信息修改成功!");
        }

        [HttpPost]
        [AdminOnlineFilter]
        public string Deleted(List<int> ids)
        {
            var ajax = new AjaxResult();
            try
            {
                Base_CodeDataService.DeleteBase_CodeData(ids, LoginInfo.LoginId);
                CommonCache.CodeDataByCache = null;
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }

            return ajax.Suc("信息已成功删除!");
        }

        [AdminOnlineFilter]
        public ActionResult Ztree()
        {
            return View();
        }

        [AdminOnlineFilter(Url = "/BaseArea/CodeData/Ztree")]
        public JsonResult ZtreeData(int? type = null)
        {
            var list = Base_CodeDataService.GetBase_CodeDataListByType(type);
            var data = list.Select(x => new ZtreeNode
                {
                    id = x.ID,
                    pId = x.CodeType,
                    name = x.CodeName,
                    isParent = x.CodeType == null
                }).ToList();

            return Json(data);
        }
    }
}
